//
//  ClubGameWelfareView.swift
//  77Club
//
//  Created by Apple on 2024/11/6.
//

import UIKit

class ClubGameWelfareTopView: UIView {

    private let giftView = c_make_ex(UIView.self)
        .bgColor("#FFF8F0").corner(8.0.Scale).end()
    private let giftImageView = c_make_ex(UIImageView.self)
        .image("GameDetail_gift").end()
    private let giftTitleLab = c_make_ex(UILabel.self)
        .color("#323233").font(16.font_semiBold)
        .text("礼包").end()
    private let giftSubLab = c_make_ex(UILabel.self)
        .color("#4B4C4D").font(14.font_medium)
        .text("专属游戏大礼包").end()
    private let couponView = c_make_ex(UIView.self)
        .bgColor("#FFF8F0").corner(8.0.Scale).end()
    private let couponImageView = c_make_ex(UIImageView.self)
        .image("GameDetail_coupon").end()
    private let couponTitleLab = c_make_ex(UILabel.self)
        .color("#323233").font(16.font_semiBold)
        .text("优惠券").end()
    private let couponSubLab = c_make_ex(UILabel.self)
        .color("#4B4C4D").font(14.font_medium)
        .text("领取优惠券").end()
    private let openView = c_make_ex(UIView.self)
        .bgColor("#FFFAF5").corner(8.0.Scale)
        .border("#FFF4E5", width: 1).end()
    private let openImageView = c_make_ex(UIImageView.self)
        .image("GameDetail_alert").end()
    private let openLabel = c_make_ex(UILabel.self)
        .color("#595959").font(14.font_regular)
        .text("开服动态：游戏内动态开服，请以游戏内为准").end()
    private let activeButton = c_make_ex(UIButton.self)
        .color("#4B4C4D").color("#323233", state: .selected)
        .font(14.font_semiBold).text("平台活动").end()
    private let openButton = c_make_ex(UIButton.self)
        .color("#4B4C4D").color("#323233", state: .selected)
        .font(14.font_semiBold).text("开服").end()
    private let activeLine = c_make_ex(UIView.self)
        .bgColor("#FD8B00").end()
    private let openLine = c_make_ex(UIView.self)
        .bgColor("#FD8B00").end()
    
    var updateLayout: ((_ isGift: Bool) -> Void)?
    var giftBlock: (() -> Void)?
    var couponBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            giftView.subviews{
                giftImageView
                giftTitleLab
                giftSubLab
            }
            couponView.subviews{
                couponImageView
                couponTitleLab
                couponSubLab
            }
            openView.subviews{
                openImageView
                openLabel
            }
            activeButton
            openButton
            activeLine
            openLine
        }
        
        let w = (CGFloat.screenWidth - 36.0.Scale)/2
        giftView.leading(12.0.Scale).top(12.0.Scale).width(w).height(70.0.Scale)
        giftImageView.leading(12.0.Scale).size(32.0.Scale).centerVertically()
        giftTitleLab.leading(52.0.Scale).top(16.0.Scale)
        giftSubLab.leading(52.0.Scale).bottom(16.0.Scale)
        couponView.trailing(12.0.Scale).top(12.0.Scale).width(w).height(70.0.Scale)
        couponImageView.leading(12.0.Scale).size(32.0.Scale).centerVertically()
        couponTitleLab.leading(52.0.Scale).top(16.0.Scale)
        couponSubLab.leading(52.0.Scale).bottom(16.0.Scale)
        openView.leading(12.0.Scale).trailing(12.0.Scale).height(40.0.Scale)
        openView.Top == giftView.Bottom + 16.0.Scale
        openImageView.leading(8.0.Scale).size(22.0.Scale).centerVertically()
        openLabel.leading(30.0.Scale).trailing(10.0.Scale).centerVertically()
        activeButton.leading(12.0.Scale).width(72.0.Scale).height(14.0.Scale)
        activeButton.Top == giftView.Bottom + 24.0.Scale
        openButton.width(44.0.Scale).height(14.0.Scale)
        openButton.CenterY == activeButton.CenterY
        openButton.Leading == activeButton.Trailing + 16.0.Scale
        activeLine.leading(21.0.Scale).height(2).width(56.0.Scale)
        activeLine.Top == activeButton.Bottom + 6.0.Scale
        openLine.leading(109.0.Scale).height(2).width(28.0.Scale)
        openLine.Top == openButton.Bottom + 6.0.Scale
        
        activeButton.isSelected = true
        openButton.isSelected = false
        activeLine.isHidden = false
        openLine.isHidden = true
        openView.isHidden = true
        
        activeButton.addTarget(self, action: #selector(clickActiveButton), for: .touchUpInside)
        openButton.addTarget(self, action: #selector(clickLineButton), for: .touchUpInside)
        
        let giftTap = UITapGestureRecognizer(target: self, action: #selector(clickGiftView))
        let couponTap = UITapGestureRecognizer(target: self, action: #selector(clickCouponView))
        giftView.isUserInteractionEnabled = true
        couponView.isUserInteractionEnabled = true
        giftView.addGestureRecognizer(giftTap)
        couponView.addGestureRecognizer(couponTap)
    }
    
    @objc private func clickGiftView(){
        giftBlock?()
    }
    
    @objc private func clickCouponView(){
        couponBlock?()
    }
    
    @objc private func clickActiveButton(){
        
        if !activeButton.isSelected {
            
            activeButton.isSelected = true
            openButton.isSelected = false
            activeLine.isHidden = false
            openLine.isHidden = true
            
            openView.isHidden = true
            activeButton.topConstraint?.constant = 24.0.Scale
            self.updateLayout?(true)
        }
    }
    
    @objc private func clickLineButton(){
        
        if !openButton.isSelected {
            
            openButton.isSelected = true
            activeButton.isSelected = false
            openLine.isHidden = false
            activeLine.isHidden = true
            
            openView.isHidden = false
            activeButton.topConstraint?.constant = 80.0.Scale
            self.updateLayout?(false)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class ClubGameWelfareActiveCell: UITableViewCell {
    
    private let backView = c_make_ex(UIView.self)
        .bgColor("#F7F8FA").corner(12.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#323233").font(15.font_semiBold).line(2).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#808080").font(11.font_medium).end()
    private let bgImageView = c_make_ex(UIImageView.self)
        .corner(8.0.Scale).mode(.scaleAspectFill).end()
    private let tagImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("GameDetail_tag").end()
    
    var model: ClubGameDetailWelfareModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.title
            timeLabel.text = md.time
            bgImageView.loadImage(url: md.image)
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                nameLabel
                timeLabel
                bgImageView.subviews{
                    tagImageView
                }
            }
        }
        
        backView.leading(12.0.Scale).trailing(12.0.Scale).top(16.0.Scale).bottom(0)
        nameLabel.leading(16.0.Scale).top(16.0.Scale).trailing(16.0.Scale)
        timeLabel.leading(16.0.Scale).trailing(16.0.Scale)
        timeLabel.Top == nameLabel.Bottom + 4.0.Scale
        bgImageView.leading(16.0.Scale).trailing(16.0.Scale).bottom(16.0.Scale).height(152.0.Scale)
        bgImageView.Top == timeLabel.Bottom + 12.0.Scale
        tagImageView.trailing(0).top(0).width(49.0.Scale).height(20.0.Scale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}
